package cn.com.chengmandian.core.db.pojo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Data
@ApiModel("分页数据结果")
public class PageData<T> implements Serializable {

    @ApiModelProperty("页码")
    private long pageNumber;

    @ApiModelProperty("分页大小")
    private long pageSize;

    @ApiModelProperty("数据")
    private List<T> rows;

    @ApiModelProperty("数据总数")
    private long total;

    public PageData(long pageNumber, long pageSize, List<T> rows, long total) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.rows = rows;
        this.total = total;
    }

    /**
     * 根据IPage组装分页返回数据
     */
    public static<T> PageData<T> build(IPage<T> page) {
        return new PageData(page.getCurrent(), page.getSize(), page.getRecords(), page.getTotal());
    }

    /**
     * 返回空的分页数据
     */
    public static<T> PageData<T> empty(){
        return new PageData(1, 20, new ArrayList<T>(), 0);
    }
}
